﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;

public partial class CheckPoint : System.Web.UI.Page
{
    public int AddOrUpdateSign = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");


        if (!Page.IsPostBack)
        {
            DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
            this.gdvList.DataSource = checkpointdals.GetMyList();

            string[] keys ={ "CheckPointID" };
            gdvList.DataKeyNames = keys;
            this.gdvList.DataBind();


            DAL.GroupDALs groupdals = new DAL.GroupDALs();
            this.DropGroup.DataSource = BLL.BGroup.GetMyList(0);
            this.DropGroup.DataBind();
        }
    }

  

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvList.PageIndex = e.NewPageIndex;
        DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
        this.gdvList.DataSource = checkpointdals.GetMyList();

        string[] keys ={ "CheckPointID" };
        gdvList.DataKeyNames = keys;
        this.gdvList.DataBind();


        DAL.GroupDALs groupdals = new DAL.GroupDALs();
        this.DropGroup.DataSource = BLL.BGroup.GetMyList(0);
        this.DropGroup.DataBind();

    }
    
    protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.RowIndex].Value);
        DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
        checkpointdals.DeleteCheckPoints(id);
        Response.Redirect("CheckPoint.aspx");
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.NewEditIndex].Value);
        DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
        Entities.CheckPoint checkpoint = checkpointdals.GetByID(id);
        if (checkpoint == null)
            return;

        this.TxtSentMessage.Text = checkpoint.SendMessage;
        this.CbISSentEmail.Checked = checkpoint.IsSendEmail;
        this.CbISSentSMS.Checked = checkpoint.IsSendSMS;
        //this.TxtStartTime.Text = checkpoint.StartTime;
       // this.TxtEndTime.Text = checkpoint.EndTime;
       // this.TxtCheckValue.Text = checkpoint.CheckValue;
       // this.TxtPeriods.Text = checkpoint.Periods.ToString();
        //this.TxtLogNum.Text = checkpoint.LogNum.ToString();
        this.txtTitle.Text = checkpoint.F2;

        this.ddlStatus.SelectedItem.Selected = false;
        if (checkpoint.Status == 0)
        {
            this.ddlStatus.Items[0].Selected = true;
        }
        else
        {
            this.ddlStatus.Items[1].Selected = true;
        }

        this.DropGroup.SelectedItem.Selected = false;
        if (DropGroup.Items.FindByValue(checkpoint.GroupID.ToString()) != null)
        {
            DropGroup.SelectedItem.Selected = false;
            DropGroup.Items.FindByValue(checkpoint.GroupID.ToString()).Selected = true;
        }
        ViewState["CheckPointID"] = checkpoint.CheckPointID;
        this.Button1.Text = "修改";
    }
   


    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //protected void Btn_Add(object sender, EventArgs e)
    //{
    //    DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
    //    checkpointdals.Add(false, CbISSentEmail.Checked, short.Parse(ddlStatus.SelectedValue),TxtSentMessage.Text, "",
    //        0,null, null, 0,
    //        int.Parse(DropGroup.SelectedValue));

    //    Response.Redirect("CheckPoint.aspx");

    //}

    protected void Button1_Click1(object sender, EventArgs e)
    {
       
        DAL.CheckPointDALs checkpointdals = new DAL.CheckPointDALs();
        Entities.CheckPoint checkpoint;
        if (this.Button1.Text == "修改")
        {
            checkpoint = checkpointdals.GetByID(Convert.ToInt16(ViewState["CheckPointID"]));
        }
        else
            checkpoint = new Entities.CheckPoint();

        checkpoint.SendMessage = this.TxtSentMessage.Text;
        checkpoint.IsSendEmail = this.CbISSentEmail.Checked;
        checkpoint.IsSendSMS = this.CbISSentSMS.Checked;
        //checkpoint.StartTime = this.TxtStartTime.Text;
        //checkpoint.EndTime = this.TxtEndTime.Text;
        //checkpoint.CheckValue = this.TxtCheckValue.Text;
        checkpoint.F2 = this.txtTitle.Text;
        //if (this.TxtPeriods.Text != null && this.TxtPeriods.Text != "")
        //{
        //    checkpoint.Periods = int.Parse(this.TxtPeriods.Text);

        //}
        //else
        //    checkpoint.Periods = 0;

        checkpoint.GroupID = int.Parse(this.DropGroup.SelectedItem.Value);

        checkpoint.Status = short.Parse(this.ddlStatus.SelectedItem.Value);
        if (this.Button1.Text == "修改")
            checkpointdals.CreateUpdateDeleteCheckPoint(checkpoint, DAL.DataProviderAction.Update);
        else
        {
            checkpoint.CheckPointID = Convert.ToInt16(ViewState["CheckPointID"]);
            checkpointdals.CreateUpdateDeleteCheckPoint(checkpoint, DAL.DataProviderAction.Create);
        }
        Response.Redirect("CheckPoint.aspx");

    }

}
